package tcpdemo.tcptest2;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket implements Runnable{
    private Socket accept;
    public ThreadSocket(Socket accept) {
        this.accept = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            // 从客户端读取数据
            BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
            // 将数据写入到本地
            bos = new BufferedOutputStream(new FileOutputStream("UdpDemo\\ServerDir\\"+ UUID.randomUUID().toString() + ".jpg"));

            int b;
            while ((b = bis.read())!= -1){
                bos.write(b);
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功");
            bw.newLine();
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (accept != null){
                try {
                    accept.close();
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
